"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
const fs = __importStar(require("fs"));
const path_1 = __importDefault(require("path"));
const vue_1 = require("vue");
const icons_vue_1 = require("@element-plus/icons-vue");
const lib_css_1 = __importDefault(require("../../../../../../@libs/nodejs-lib-main/lib_css"));
const config_1 = __importDefault(require("../../../../../config"));
const component = {
    components: { Plus: icons_vue_1.Plus, Location, Delete: icons_vue_1.Delete, Download: icons_vue_1.Download, ZoomIn: icons_vue_1.ZoomIn },
    name: 'upload_location_pic',
    props: {
        currentLocation: null,
    },
    template: fs.readFileSync(`${__dirname}/upload_location_pic.html`, "utf-8"),
    methods: {
        handleRemove(file, fileList) {
            this.$data.fileList = [];
            this.$data.projectUrl = null;
        },
        handlePictureCardPreview(file) {
            this.$data.dialogImageUrl = file.url;
            this.$data.dialogVisible = true;
        },
        handleDownload(file) {
            console.log(file);
        },
        handleChange(file) {
            console.log("file", file);
            const reader = new FileReader();
            const saveFolderPath = path_1.default.join(config_1.default.script_path_s, "/pic");
            const fileName = `${saveFolderPath}/${file.name}`;
            // 创建目标文件夹（如果不存在）
            if (!fs.existsSync(saveFolderPath)) {
                fs.mkdirSync(saveFolderPath, { recursive: true });
            }
            reader.onload = (e) => {
                const result = e.target.result;
                if (typeof result === 'string') {
                    // 如果是字符串，可以直接传递给fs.writeFile
                    const contents = result;
                    fs.writeFile(fileName, contents, (err) => {
                        if (err) {
                            console.error('保存文件出错', err);
                        }
                        else {
                            console.log('文件已保存到', fileName);
                        }
                    });
                }
                else if (result instanceof ArrayBuffer) {
                    // 如果是ArrayBuffer，将其转换为Buffer再传递给fs.writeFile
                    const contents = Buffer.from(result);
                    fs.writeFile(fileName, contents, (err) => {
                        if (err) {
                            console.error('保存文件出错', err);
                        }
                        else {
                            console.log('文件已保存到', fileName);
                        }
                    });
                }
                else {
                    console.error('不支持的文件内容类型');
                }
            };
            reader.readAsArrayBuffer(file.raw);
            this.$emit("savePic", fileName);
        },
        // 上传成功后的操作;
        handleSuccess(res, file, fileList) {
            this.$data.fileList = [...fileList];
        },
    },
    data() {
        return {
            fileList: this.$props.currentLocation.location_picUrl ? (0, vue_1.ref)([
                {
                    name: this.$props.currentLocation.location_name + ".jpg",
                    url: this.$props.currentLocation.location_picUrl,
                },
            ]) : (0, vue_1.ref)([]),
            uploadUrl: config_1.default.resources_path_s,
            limit: 1,
            // uploadFileNum:0,
            // maxUploadNum:1,
            projectUrl: (0, vue_1.ref)(''),
            dialogImageUrl: (0, vue_1.ref)(''),
            dialogVisible: (0, vue_1.ref)(false),
            disabled: (0, vue_1.ref)(false),
            input: (0, vue_1.ref)(''),
        };
    },
    computed: {
        hideUpload() {
            return this.$data.fileList.length >= this.$data.limit;
        },
    },
    watch: {},
    created() {
    },
    mounted() {
        lib_css_1.default.load([
            {
                parent: this.$el,
                url_s: path_1.default.join(`${__dirname}/upload_location_pic.css`),
            },
        ]);
    },
};
module.exports = component;
